package com.cuanbobox.tcp.matrix;

import com.cuanbobox.base.App;
import com.cuanbobox.constant.MatrixConstant;
import com.cuanbobox.service.BackgroundService;
import com.cuanbobox.utils.BroadcastUtil;
import com.cuanbobox.utils.LogUtil;

import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;

/**
 * 矩阵初始化线程
 */
public class MatrixInitThread extends Thread {
    private String ip;
    private int port;
    private Socket socket;
    //Socket是否正在连接
    public boolean isConnenting;
    //Socket是否已连接
    public boolean isConnected;
    private MatrixReceiveThread tcpReceiveThread;
    private MatrixSendThread tcpSendThread;
    private MatrixHeartbeatThread tcpHeartbeatThread;

    public MatrixInitThread(String ip, int port) {
        this.ip = ip;
        this.port = port;
        isConnenting = true;
        isConnected = false;
    }

    @Override
    public void run() {
        super.run();
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        startConnect();
    }

    public void close() {
        if (socket != null) {
            try {
                tcpSendThread.stopThread();
                tcpReceiveThread.stopThread();
                tcpHeartbeatThread.stopThread();
                socket.close();
                socket = null;
                isConnected = false;
                BackgroundService.reConnectingMatrix = false;
            } catch (Exception e) {

            }
        }
    }

    private void startConnect() {
        try {
            LogUtil.add("建立连接", "开始连接！", "矩阵");
            socket = new Socket(ip, Integer.valueOf(port));
            socket.setKeepAlive(true);
            //设置心跳包
            //socket.sendUrgentData(0xFF);
            if (socket.isConnected()) {
                isConnected = true;
                LogUtil.add("建立连接", "连接成功！", "矩阵");
                BroadcastUtil.sendMatrixBroadcast(MatrixConstant.TYPE_MATRIX_INIT_CONNECT_SUCCESS);
                tcpSendThread = new MatrixSendThread(this);
                tcpSendThread.start();
                tcpReceiveThread = new MatrixReceiveThread(this);
                tcpReceiveThread.start();
                tcpHeartbeatThread = new MatrixHeartbeatThread(this);
                tcpHeartbeatThread.start();
            }
        } catch (ConnectException e) {//连接失败，请检查IP和端口号是否正确
            isConnected = false;
            BackgroundService.reConnectingMatrix = false;
            BroadcastUtil.sendMatrixBroadcast(MatrixConstant.TYPE_MATRIX_INIT_CONNECT_FAIL);
            LogUtil.add("建立连接", "连接失败，请检查IP和端口号是否正确！", "矩阵");
        } catch (IOException e) {
            e.printStackTrace();
            isConnected = false;
            BackgroundService.reConnectingMatrix = false;
            BroadcastUtil.sendMatrixBroadcast(MatrixConstant.TYPE_MATRIX_INIT_ERROR);
            LogUtil.add("建立连接", "连接失败，错误-->" + e.getMessage(), "矩阵");
        } finally {
            isConnenting = false;
        }
    }

    public boolean isConnenting() {
        return isConnenting;
    }

    public boolean isConnected() {
        return isConnected;
    }

    public void setConnected(boolean connected) {
        isConnected = connected;
    }

    public String getIp() {
        return ip;
    }

    public int getPort() {
        return port;
    }

    public Socket getSocket() {
        return socket;
    }
}
